package ps.client.gui.util.textpanel;

import java.awt.Color;
import java.awt.Font;


public class DefaultChatLineFormater implements ChatLineFormater {
    
    public static final Font FONT_TIME_STAMP = new Font("Courier New", Font.PLAIN, 11);
    public static final Font FONT_SENDER = new Font("Arial", Font.PLAIN, 12);
    public static final Font FONT_MESSAGE_1 = new Font("Arial", Font.PLAIN, 12);
    public static final Font FONT_MESSAGE_2 = new Font("Arial", Font.PLAIN, 12);
    
    public static final Color COLOR_TIME_STAMP = Color.GRAY;
    public static final Color COLOR_SENDER = new Color(100, 255, 150);
    public static final Color COLOR_RECIEVER = new Color(100, 255, 150);
    
    public static final Color COLOR_MESSAGE_1 = new Color(255, 255, 255);
    public static final Color COLOR_MESSAGE_2 = new Color(0, 255, 0);
    
    boolean timeStamp = true;
    
    public TextLine formatChatLine(String sender, String reciever, String msg) {
        TextLine textLine = new TextLine();
        
        if (timeStamp) {
            textLine.appendText(getCurrentTimeStr(), FONT_TIME_STAMP, COLOR_TIME_STAMP, 0, 3, 2);
        }
        
        textLine.appendText(sender + ">", FONT_SENDER, COLOR_SENDER, 0, 3, 0);
        
        if (reciever != null) {
            textLine.appendText("[" + reciever + "]", FONT_SENDER, COLOR_SENDER, 0, 3, 0);
            textLine.appendText(msg, FONT_MESSAGE_2, COLOR_MESSAGE_2, 0, 3, 0);
        } else {
            textLine.appendText(msg, FONT_MESSAGE_1, COLOR_MESSAGE_1, 0, 3, 0);
        }

        return textLine;
    }
    
    private String getCurrentTimeStr() {
        long timeSecs = System.currentTimeMillis() / 1000;
        long timeMins = timeSecs / 60;
        long timeHours = timeMins / 60 + 1;
        int hours = (int)(timeHours % 24);
        int mins = (int)(timeMins % 60);
        
        StringBuilder timeStr = new StringBuilder();
        if (hours < 10) {
            timeStr.append('0');
        }
        timeStr.append(hours);
        timeStr.append(':');
        if (mins < 10) {
            timeStr.append('0');
        }
        timeStr.append(mins);
        
        return timeStr.toString();
    }
    
    @Override
    public void setTimeStamp(boolean b) {
        this.timeStamp = b;
    }

    @Override
    public boolean isTimeStamp() {
        return timeStamp;
    }

}
